package com.xiaobao.campusLostFoundAfter.controller;

import com.github.pagehelper.PageInfo;
import com.xiaobao.campusLostFoundAfter.common.Result;
import com.xiaobao.campusLostFoundAfter.pojo.Comment;
import com.xiaobao.campusLostFoundAfter.pojo.LostItem;
import com.xiaobao.campusLostFoundAfter.service.LostItemService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/*失物控制模块*/
@RestController
@RequestMapping("/LostItem")
public class LostItemController {
    private static final Logger log = LoggerFactory.getLogger(LostItemController.class);
    private final LostItemService lostItemService;

    public LostItemController(LostItemService lostItemService) {
        this.lostItemService = lostItemService;
    }

    /*分页查询所有失物*/

    /**
     * 分页查询失物接口
     *
     * @param categoryId 分类ID，0表示所有分类
     * @param page       页码，从1开始
     * @param pageSize   每页记录数
     * @param keyword    搜索关键词
     * @return 分页查询结果
     */
    @GetMapping("/page")
    public Result<PageInfo<LostItem>> getLostItemsByPage(
            @RequestParam(defaultValue = "0") Integer categoryId,
            @RequestParam(defaultValue = "-1") Integer status,
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer pageSize,
            @RequestParam(defaultValue = "") String keyword) {

        // 调用服务层获取分页数据
        PageInfo<LostItem> pageInfo = lostItemService.getLostItemByPage(
                page, pageSize, categoryId, status, keyword);
        return Result.success(pageInfo);
    }
    //获取全部的分页
    @GetMapping("/getAllPage")
    public Result<PageInfo<LostItem>> getAllLostItemsByPage(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer pageSize) {

        // 调用服务层获取分页数据
        PageInfo<LostItem> pageInfo = lostItemService.getAllLostItemsByPage(page, pageSize);
        return Result.success(pageInfo);
    }

    /*查询失物总数*/
    @GetMapping("/count")
    public Result<Integer> getLostItemCount() {
        int count = lostItemService.selectLostItemCount();
        return Result.success(count);
    }

    /*添加失物信息*/
    @PostMapping("/add")
    public Result<String> addLostItem(@RequestBody LostItem lostItem) {
        lostItem.setCreateTime(new Date());
        lostItem.setUpdateTime(new Date());
        int result = lostItemService.addLostItem(lostItem);
        if (result == 1) {
            return Result.success();
        } else {
            return Result.error("添加失败");
        }
    }

    /*更新失物信息*/
    @PutMapping("/update")
    public Result<String> updateLostItem(@RequestBody LostItem lostItem) {
        int result = lostItemService.updateLostItem(lostItem);
        if (result == 1) {
            return Result.success();
        } else {
            return Result.error("更新失败");
        }
    }

    /*删除失物信息*/
    @DeleteMapping("/delete/{id}")
    public Result<String> deleteLostItem(@PathVariable Integer id) {
        int result = lostItemService.deleteLostItem(id);
        if (result == 1) {
            return Result.success();
        } else {
            return Result.error("删除失败");
        }
    }

    /*根据ID查询失物信息*/
    @GetMapping("/{id}")
    public Result<LostItem> getLostItemById(@PathVariable("id") Integer id) {
        LostItem lostItem = lostItemService.getLostItemById(id);
        if (lostItem != null) {
            return Result.success(lostItem);
        } else {
            return Result.error("未找到该失物信息");
        }
    }

    /**
     * 添加评论接口
     * 该方法通过POST请求接收一个Comment对象作为参数，用于在某个物品下添加评论
     * 主要涉及对Comment对象中内容、用户ID和物品ID的处理，以完成评论的添加操作
     *
     * @param comment 包含评论内容、用户ID和物品ID的Comment对象，通过请求体传递
     * @return 返回一个Result对象，包含操作结果信息
     */
    @PostMapping("/addComment")
    public Result<String> addComment(@RequestBody Comment comment) {
        // 调用服务层方法，尝试添加评论
        int result = lostItemService.addComment(comment.getContent(), comment.getUserId(), comment.getItemId());

        // 根据添加结果返回相应的信息
        if (result == 1) {
            // 如果添加成功，返回成功信息
            return Result.success();
        } else {
            // 如果添加失败，返回错误信息
            return Result.error("添加失败");
        }
    }

    //获取物品的所有评论
    @GetMapping("/getComments/{itemId}")
    public Result<List<Comment>> getComments(@PathVariable Integer itemId) {
        List<Comment> comments = lostItemService.getComments(itemId);
        if (comments != null) {
            return Result.success(comments);
        } else {
            return Result.error("未找到该物品的评论");
        }
    }
    //下架物品
    @PutMapping("/approve/{id}")
    public Result<String> updateStatus(@PathVariable Integer id, @RequestParam(defaultValue = "2") String status) {
        //将String转化为int
        Integer statusInt = Integer.parseInt(status);
        int result = lostItemService.updateStatus(id,statusInt);
        if (result == 1) {
            return Result.success();
        } else {
            return Result.error("更新失败");
        }
    }
    @PutMapping("/reject/{id}")
    public Result<String> reject(@PathVariable Integer id, @RequestParam(defaultValue = "5") String status) {
        //将String转化为int
        Integer statusInt = Integer.parseInt(status);
        int result = lostItemService.updateStatus(id,statusInt);
        if (result == 1) {
            return Result.success();
        } else {
            return Result.error("更新失败");
        }
    }
    //获取所有与为审核和驳回的物品
    //分页
    @GetMapping("/getAllRecord")
    public Result<PageInfo<LostItem>> getAllRecord(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer pageSize) {
        // 调用服务层获取分页数据

        PageInfo<LostItem> pageInfo = lostItemService.getAllOrderByPage(page, pageSize);
        return Result.success(pageInfo);
    }
}
